package com.marry.core.aop;

import cn.hutool.core.lang.UUID;
import com.marry.core.bean.BaseBean;
import com.marry.core.utils.DateUtil;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * @path：com.marry.common.core.aop.MapperAspect.java
 * @className：MapperAspect.java
 * @description：mapper aop
 * @author：LiuYun
 * @dateTime：2021/06/29 22:32
 * @editNote：
 */
@Aspect
@Component
@Order(0)
public class MapperAspect {

    @Before(value = "(execution(* com.marry..*.repository..*.insert*(..)) || execution(* com.marry..*.repository..*.save*(..))) && args(t)", argNames = "t")
    public <T extends BaseBean> void insert(T t) {
        if(ObjectUtils.isEmpty(t.getEffective())) {
            t.setEffective("1");
        }
        // TODO 设置创建者
        // TODO 设置更新者
        t.setCreateDate(DateUtil.getDate());
        t.setCreateTime(DateUtil.getTime());
        t.setUpdateDate(DateUtil.getDate());
        t.setUpdateTime(DateUtil.getDate());
    }

    @Before(value = "(execution(* com.marry..*.repository..*.update*(..)) || execution(* com.marry..*.repository..*.modify*(..))) && args(t)", argNames = "t")
    public <T extends BaseBean> void update(T t) {
        // TODO 设置更新者
        t.setUpdateDate(DateUtil.getDate());
        t.setUpdateTime(DateUtil.getDate());
    }
}
